﻿using Aker.DevTeam;
using DevTeam;
using Extensions;
using System;
using System.Data;

namespace Aker.Sistem
{
    public partial class CariAktarma : XForm
    {
        public CariAktarma()
        {
            InitializeComponent();
        }

        AkerDB.CariAktarimDataTable dt = new AkerDB.CariAktarimDataTable();
        private void CariAktarma_Load(object sender, EventArgs e)
        { 
            Data.TAM.CariAktarimTableAdapter.Fill(dt);
            gv.Init(dt);

            gv.BestFitColumns();
        }

        private void gv_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {

        }

        private void Kaydet_Click(object sender, EventArgs e)
        {
            Data.TAM.CariAktarimTableAdapter.Update(dt);
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {

            Do(1);
        }

        private void Do(int tip)
        {
            DataManager dm = new DataManager(Data.DM.ExecuteDataTable("GetConnectionString", tip).Rows[0][0].ToStringNN());

            int[] rhs = gv.GetSelectedRows();
            if (rhs.Length == 0 && gv.FocusedRowHandle > -1)
                rhs = new int[] { gv.FocusedRowHandle };

            bool found = false;
            foreach (int rh in rhs)
            {
                DataRow r = gv.GetDataRow(rh);
                if (r["tr" + tip.ToString()].ToIntNN() < 0)
                {
                    string sonuc = dm.ExecuteDataTable("AKER_CariEkle", r["Firma"], r["Kod"], r["Adres1"], r["Adres2"], r["Semt"], r["Il"], r["VergiDairesi"], r["VergiNo"]).Rows[0][0].ToStringNN();
                    if (sonuc.ToIntNN() > 0)
                    {
                        r["tr" + tip.ToString()] = sonuc.ToIntNN();
                        found = true;
                    }
                    else
                        ShowError(r["Firma"].ToStringNN() + " " + sonuc);
                }
                else
                    ShowError(r["Firma"].ToStringNN() + " daha önceden aktarılmış.");
            }

            if (found)
                Kaydet.PerformClick();
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            Do(2);
        }

        private void simpleButton3_Click(object sender, EventArgs e)
        {
            Do(3);
        }

        private void simpleButton4_Click(object sender, EventArgs e)
        {
            Do(4);
        }
    }
}
